<?php   
class ControllerCommonHeader extends Controller {
	protected function index() {
		$this->data['title'] = $this->document->getTitle();
		
		if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
			$this->data['base'] = $this->config->get('config_ssl');
		} else {
			$this->data['base'] = $this->config->get('config_url');
		}
		
		$this->data['description'] = $this->document->getDescription();
		$this->data['keywords'] = $this->document->getKeywords();
		$this->data['links'] = $this->document->getLinks();	 
		$this->data['styles'] = $this->document->getStyles();
		$this->data['scripts'] = $this->document->getScripts();
		$this->data['lang'] = $this->language->get('code');
		$this->data['direction'] = $this->language->get('direction');
		$this->data['google_analytics'] = html_entity_decode($this->config->get('config_google_analytics'), ENT_QUOTES, 'UTF-8');
		
		$this->language->load('common/header');
		
		if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
			$server = HTTPS_IMAGE;
		} else {
			$server = HTTP_IMAGE;
		}	
				
		if ($this->config->get('config_icon') && file_exists(DIR_IMAGE . $this->config->get('config_icon'))) {
			$this->data['icon'] = $server . $this->config->get('config_icon');
		} else {
			$this->data['icon'] = '';
		}
		
		$this->data['name'] = $this->config->get('config_name');
				
		if ($this->config->get('config_logo') && file_exists(DIR_IMAGE . $this->config->get('config_logo'))) {
			$this->data['logo'] = $server . $this->config->get('config_logo');
		} else {
			$this->data['logo'] = '';
		}
		
		$this->data['text_home'] = $this->language->get('text_home');
		$this->data['text_all_category'] = $this->language->get('text_all_category');
    	$this->data['text_search'] = $this->language->get('text_search');
		$this->data['text_welcome'] = sprintf($this->language->get('text_welcome'), $this->url->link('account/login', '', 'SSL'), $this->url->link('account/register', '', 'SSL'));
		$this->data['text_logged'] = sprintf($this->language->get('text_logged'), $this->customer->getFirstName(), $this->url->link('account/account', '', 'SSL'), $this->url->link('account/logout', '', 'SSL'));
				
		$this->data['home'] = $this->url->link();
		$this->data['logged'] = $this->customer->isLogged();
		
		if (isset($this->request->get['keyword'])) {
			$this->data['keyword'] = $this->request->get['keyword'];
		} else {
			$this->load->model('promotion/keyword');
			$kws = $this->model_promotion_keyword->getKeywordsForHome(1);
			if($kws){
				$this->data['keyword'] = $kws[0]['keyword'];
			}else{
				$this->data['keyword'] = "";
			}
		}
		
		$this->data['isHome'] = FALSE;
		if (!isset($this->request->get['route']) || $this->request->get['route'] == 'common/home') {
			$this->data['isHome'] = TRUE;
		}
		
		// Menu
		$this->load->model('catalog/category');
		$this->data['categories'] = array();
		$categories = $this->model_catalog_category->getCategories(0);
		foreach ($categories as $category) {
			// Level 2
			$children_2 = array();
			$children_d2 = $this->model_catalog_category->getCategories($category['category_id']);
			foreach ($children_d2 as $child2) {
				// Level 3
				$children_3 = array();
				$children_d3 = $this->model_catalog_category->getCategories($child2['category_id']);
				foreach ($children_d3 as $child3) {
					$children_3[] = array(
						'name'  => $child3['name'],
						'href'  => $this->url->linkCategory($child3['category_id'], $child3['name'])	
					);					
				}			
				$children_2[] = array(
					'name'  => $child2['name'],
					'children' => $children_3,
					'href'  => $this->url->linkCategory($child2['category_id'], $child2['name'])	
				);					
			}
			
			// Level 1
			$this->data['categories'][] = array(
				'name'     => $category['name'],
				'children' => $children_2,
				'href'     => $this->url->linkCategory($category['category_id'], $category['name'])
			);
		}
		
		$this->children = array(
			'module/language',
			'module/currency',
			'module/cart'
		);
				
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/header.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/header.tpl';
		} else {
			$this->template = 'default/template/common/header.tpl';
		}
		
    	$this->render();
	} 	
}
?>